package com.usian.article.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.mapper.ApArticleHomeMapper;
import com.usian.article.mapper.ApArticleMapper;
import com.usian.article.service.ApArticleConfigService;
import com.usian.article.service.ApArticleContentService;
import com.usian.article.service.ApArticleHomeService;
import com.usian.article.service.ApArticleService;
import com.usian.common.constant.article.LoadConstants;
import com.usian.model.article.dtos.ArticleHomeDto;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service
public class ApArticleHomeServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleHomeService {

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @Autowired
    private ApArticleContentService apArticleContentService;

    @Resource
    private ApArticleHomeMapper apArticleHomeMapper;

    private final Integer SIZE=50;

    @Override
    public ResponseResult load(ArticleHomeDto dto,Integer type) {
        //校验参数是否为空
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //设置分页
        int min = Math.min(dto.getSize(), SIZE);
        dto.setSize(min);
        //判断类型
        if (!type.equals(LoadConstants.LOADTYPE_LOAD_MORE)&&!type.equals(LoadConstants.LOADTYPE_LOAD_NEW)){
            type=LoadConstants.LOADTYPE_LOAD_MORE;
        }
        //初始时间
        if (dto.getMaxBehotTime()==null) dto.setMaxBehotTime(new Date());
        if (dto.getMinBehotTime()==null) dto.setMinBehotTime(new Date());
        List<ApArticle> apArticles = apArticleHomeMapper.load(dto, type);


        return ResponseResult.okResult(apArticles);
    }

    @Override
    public ResponseResult loadNewsById(ArticleInfoDto dto) {
        //判空
        if (dto==null||dto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id查询apArticleConfig
        ApArticleConfig apArticleConfig = apArticleConfigService.getOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId,dto.getArticleId()));
        //判定当前文章是否下架false和删除true
        System.out.println(apArticleConfig.getIsDown());
        if (apArticleConfig.getIsDown()==false || apArticleConfig.getIsDelete()==true){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id查询apArticleContent
        ApArticleContent apArticleContent = apArticleContentService.getOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId,dto.getArticleId()));
        //将apArticleContent，apArticleConfig分别保存到map集合中返回
        HashMap<Object, Object> map = new HashMap<>();
        map.put("content",apArticleContent);
        map.put("config",apArticleConfig);
        return ResponseResult.okResult(map);
    }
}